package com.tind.wyf.baselib.dialog;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.View;

import androidx.annotation.Nullable;

public class AlertDialogUtils {
    private static AlertDialogUtils mAlertDialogInstance;
    private AlertDialog.Builder builder;

    public AlertDialogUtils(@Nullable Context context) {
        builder = new AlertDialog.Builder(context);
    }

    public static AlertDialogUtils getInstance(Context context) {
        if (null == mAlertDialogInstance) {
            synchronized (AlertDialogUtils.class) {
                if (null == mAlertDialogInstance)
                    mAlertDialogInstance = new AlertDialogUtils(context);
            }

        }
        return mAlertDialogInstance;
    }

    /**
     * 消息显示
     *
     * @param title
     * @param msg
     * @return
     */
    public AlertDialog getAlertDialog(String title, String msg) {
        return getAlertDialog(title, msg, "", null);
    }

    /**
     * 只含确认消息提醒框
     *
     * @param title
     * @param msg
     * @param listener 确认
     * @return
     */
    public AlertDialog getAlertDialog(String title, String msg,
                                      String positive, DialogInterface.OnClickListener listener) {
        return getAlertDialog(title, msg, null, positive, listener);
    }

    /**
     * 消息提醒框
     *
     * @param title
     * @param msg
     * @param mNegativelistener 取消
     * @param mPositivelistener 确定
     * @return
     */
    public AlertDialog getAlertDialog(String title, String msg,
                                      DialogInterface.OnClickListener mNegativelistener,
                                      String positive, DialogInterface.OnClickListener mPositivelistener) {
        return getAlertDialog(title, msg, "", mNegativelistener, positive, mPositivelistener);
    }

    /**
     * @param title
     * @param msg
     * @param mNegativelistener 取消
     * @param mPositivelistener 确定
     * @return
     */
    public AlertDialog getAlertDialog(String title, String msg,
                                      String negative, DialogInterface.OnClickListener mNegativelistener,
                                      String positive, DialogInterface.OnClickListener mPositivelistener) {
        return builder
                .setTitle(title)
                .setMessage(msg)
                .setNegativeButton(negative, mNegativelistener)
                .setPositiveButton(positive, mPositivelistener)
                .create();
    }

    /**
     * 自定义dialog 弹窗布局
     *
     * @param context
     * @param view
     * @return
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public AlertDialog getAlertDialog(Context context, View view) {
        return getAlertDialog(context, 0, view);
    }

    /**
     * 自定义dialog
     *
     * @param context
     * @param theme
     * @param view
     * @return
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public AlertDialog getAlertDialog(Context context, int theme, View view) {
        return new AlertDialog.Builder(context, theme)
                .setView(view)
                .create();
    }

}
